//
//  UUWalletManager.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 22/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUWalletManager.h"
#import <UUFoundation/DKDataManager.h>
#import "UUWalletRecordRequestModel.h"
#import "UUWalletWithdrawRequestModel.h"
#import "UUWalletWithdrawDetailRequestModel.h"
#import "UUWalletRefundRequestModel.h"

@implementation UUWalletManager

- (void)asyncFetchRecordsForType:(UUWalletRecordType)type
                            page:(int)page
                          length:(int)length
                   completeBlock:(void (^)(NSArray<UUWalletRecordModel *> *records, NSError *error))completeBlock {
    UUWalletRecordRequestModel *requestModel = [UUWalletRecordRequestModel new];
    requestModel.type = type;
    requestModel.page = page;
    requestModel.perPage = length;
    
    [[DKDataManager new] requestWithRequestModel:requestModel
                                  completedBlock:^(DKResponseModel *responseModel, NSError *error) {
                                      if (error == nil) {
                                          UUWalletRecordResponseModel *walletResponseModel = (UUWalletRecordResponseModel *)responseModel;
                                          BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, walletResponseModel.data, nil);
                                      } else {
                                          BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, nil, error);
                                      }
                                  }];
}

- (void)asyncWithdrawWithAccountType:(UUWalletAccountType)accountType
                                name:(NSString *)name
                             account:(NSString *)account
                               money:(NSUInteger)money
                            bankName:(NSString *)bankName
                         depositBank:(NSString *)depositBank
                       completeBlock:(void (^)(NSError *error))completeBlock {
    UUWalletWithdrawRequestModel *requestModel = [UUWalletWithdrawRequestModel new];
    requestModel.accountType = accountType;
    requestModel.name = name;
    requestModel.account = account;
    requestModel.money = money;
    requestModel.bankName = bankName;
    requestModel.depositBank = depositBank;
    
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, error);
    }];
}

- (void)asyncFetchRecordDetailWithRecrodId:(NSNumber *)recordId completeBlock:(void (^)(UUWalletRecordDetailModel *detail, NSError *error))completeBlock {
    UUWalletWithdrawDetailRequestModel *requestModel = [UUWalletWithdrawDetailRequestModel new];
    requestModel.recordId = recordId;
    
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, (UUWalletRecordDetailModel *)responseModel, error);
        } else {
            BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, nil, error);
        }
    }];
}

- (void)asyncFetchRefundDetailWithRecordId:(NSNumber *)recordId
                                moneyLogId:(NSNumber *)moneyLogId
                             completeBlock:(void (^)(UUWalletRefundDetailModel *detail, NSError *error))completeBlock {
    UUWalletRefundRequestModel *requestModel = [UUWalletRefundRequestModel new];
    requestModel.moneyLogId = moneyLogId;
    requestModel.recordId = recordId;
    
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, (UUWalletRefundDetailModel *)responseModel, nil);
        } else {
            BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, nil, error);
        }
    }];
}

@end
